package cn.tedu.baking.controller;

import cn.tedu.baking.mapper.ContentMapper;
import cn.tedu.baking.pojo.dto.ContentDTO;
import cn.tedu.baking.pojo.entity.Content;
import cn.tedu.baking.pojo.vo.ContentIndexVO;
import cn.tedu.baking.pojo.vo.ContentManagementVO;
import cn.tedu.baking.pojo.vo.ContentUpdateVO;
import cn.tedu.baking.response.JsonResult;
import cn.tedu.baking.security.CustomUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/contents/")
public class ContentController {
    @Value("${filePath}")
    private String dirPath;

    @Autowired
    ContentMapper mapper;

    @PostMapping("add-new")
    public JsonResult addNew(@RequestBody ContentDTO contentDTO,
                             @AuthenticationPrincipal CustomUserDetails customUserDetails){
        log.info("添加新内容：{}",contentDTO);
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);
        //判断是否传入id来决定修改还是插入
        if (content.getId()!=null) {//修改
            content.setUpdateTime(new Date());
            content.setUpdateBy(customUserDetails.getId());
            mapper.update(content);
        }else {
            content.setCreateTime(new Date());
            mapper.insert(content);//插入
        }
        return JsonResult.ok();
    }

    @GetMapping("{type}/management")
    public JsonResult management(@PathVariable Integer type,
                                 @AuthenticationPrincipal CustomUserDetails customUserDetails){
        List<ContentManagementVO> contentManagementVOS = mapper.selectByType(type, customUserDetails.getId());
        log.debug(contentManagementVOS.toString());
        //通过传递过来的type和当前登录的用户id查询相关内容
        return JsonResult.ok(contentManagementVOS);
    }

    @GetMapping("{id}/update")
    public JsonResult selectForUpdate(@PathVariable Long id){
        return JsonResult.ok(mapper.selectByIdForUpdate(id));
    }

    @PostMapping("{id}/delete")
    public JsonResult delete(@PathVariable Long id){
        //得到封面路径删除
        ContentUpdateVO contentUpdateVO = mapper.selectByIdForUpdate(id);
        new File(dirPath+contentUpdateVO.getImgUrl()).delete();
        if (contentUpdateVO.getType()==2) {//判断如果是视频则删除视频文件
            new File(dirPath+contentUpdateVO.getVideoUrl()).delete();
        }

        mapper.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("{type}/{categoryId}/index")
    public JsonResult selectIndex(@PathVariable Integer type,
                                  @PathVariable Long categoryId){

        List<ContentIndexVO> list = mapper.selectByTypeAndCategoryId(type,categoryId);
        return JsonResult.ok(list);
    }

    @GetMapping("{id}/detail")
    public JsonResult selectDetail(@PathVariable Long id){
        //让浏览量+1
        mapper.updateViewCountById(id);
        return JsonResult.ok(mapper.selectBfyIdForDetail(id));
    }

    @GetMapping("/{userId}/other")
    public JsonResult selectOther(@PathVariable Long userId){

        return JsonResult.ok(mapper.selectByUserId(userId));
    }

    @GetMapping("hot")
    public JsonResult selectHot(){
        return JsonResult.ok(mapper.selectHot());
    }

    @GetMapping("{type}/list")
    public JsonResult selectList(@PathVariable Integer type){
        return JsonResult.ok(mapper.selectListByType(type));
    }

    @GetMapping("{wd}/search")
    public JsonResult search(@PathVariable String wd){
        return JsonResult.ok(mapper.searchByWd(wd));
    }

    @GetMapping("{type}/admin")
    public JsonResult selectAdmin(@PathVariable Integer type){
        return JsonResult.ok(mapper.selectByTypeForAdmin(type));
    }

}
